package trabajoISW2.domain.implementations;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import trabajoISW2.domain.contracts.ICatalog;
import trabajoISW2.domain.exceptions.WrongNameException;


public class Catalog implements ICatalog{
	
	
	private List<Book> catalog;

	
	//Constructor
	public Catalog(List<Book> books){
		this.catalog = books;
	}
	
	public Catalog(){
		super();
		this.catalog = new LinkedList<Book>();
	}
	
	//Funciones de ICatalogModel
	
	
	@Override
	public boolean areBooksAuthor(List<Book> books, int authorId) {
		
		Book book;
		boolean b = true;
		ListIterator<Book> it = (ListIterator<Book>) books.iterator();
		while (it.hasNext() && b){
			book = it.next();
			if(book.getAuthor().getAuthorId() != authorId)
				b = false;
							
		}
		return b;
	}

	@Override
	public boolean areBooksTitle(List<Book> books, String title) {
		Book book;
		boolean b = true;
		ListIterator<Book> it = (ListIterator<Book>) books.listIterator();
		while (it.hasNext() && b){
			book = it.next();
			if(!book.getTitle().equals(title))
				b = false;
		}
		return b;
	}

	
	// Funciones de ICatalog
	@Override
	public void setBooks(List<Book> books) {
		if(books == null)
			throw new IllegalArgumentException();
		else
			this.catalog = books;
	}

	@Override
	public List<Book> getBooks() {

		return this.catalog;
	}

	@Override
	public List<Book> searchByAuthorId(int authorId) {
		if(authorId<0|authorId>Integer.MAX_VALUE)
			throw new IllegalArgumentException();
		else{
		Book b;
		List<Book> result = new LinkedList<Book>();
		ListIterator<Book> it = catalog.listIterator();
		
		while(it.hasNext()){
			b = it.next();
			if(b.getAuthor().getAuthorId() == authorId)
				result.add(b);
		}
		
		return result;
		}
	}

	@Override
	public List<Book> searchByTitle(String title) {
		if(title == null | title == "")
			throw new WrongNameException();
		else{
		
		Book b;
		List<Book> result = new LinkedList<Book>();
		ListIterator<Book> it = catalog.listIterator();
		
		while(it.hasNext()){
			b = it.next();
			if(b.getTitle().equals(title))
				result.add(b);
		}
		
		return result;		
		}
	}

	@Override
	public Book getBook(String isbn) {
		Book book = null;
		if(isbn == null || isbn.equals(" ") )
			throw new IllegalArgumentException();
		else{
		ListIterator<Book> it = catalog.listIterator();
		
		boolean b = true;
		
		while (it.hasNext() && b){
			book = it.next();
			if(book.getIsbn().equals(isbn))
				b = false;
						
		}
			}
		return book;
	}

	@Override
	public boolean addBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else{
		boolean b = true;
		if (this.getBook(book.getIsbn()) == null)
			this.catalog.add(book);
		else
			b = false;
		
		return b;
		}
			
	}

	@Override
	public boolean removeBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		boolean b = true;
		if (this.getBook(book.getIsbn()) != null)
			this.catalog.remove(book);
		else
			b = false;
		
		return b;
	}



}
